<?php

namespace app\common\model\order;

use think\Model;
use traits\model\SoftDelete;

class OrderItem extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order_item';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'aftersale_status_text',
        'comment_status_text',
        'refund_status_text',
        'rights_endtime_text'
    ];

    public function getRightsEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rights_endtime']) ? $data['rights_endtime'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }
    
    public function getAftersaleStatusList()
    {
        return ['-1' => __('Aftersale_status -1'), '0' => __('Aftersale_status 0'), '1' => __('Aftersale_status 1'), '2' => __('Aftersale_status 2')];
    }

    public function getCommentStatusList()
    {
        return ['0' => __('Comment_status 0'), '1' => __('Comment_status 1')];
    }

    //退款状态:-1=拒绝退款,0=无,1=申请中,2=同意
    public function getRefundStatusList()
    {
        return ['-1' => __('拒绝退款'), '0' => __('无'), '1' => __('申请中'), '2' => __('同意')];
    }


    public function getAftersaleStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['aftersale_status']) ? $data['aftersale_status'] : '');
        $list = $this->getAftersaleStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCommentStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['comment_status']) ? $data['comment_status'] : '');
        $list = $this->getCommentStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function order()
    {
        return $this->belongsTo('app\common\model\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
